package sqlToExcel;

import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineConfig;
import cn.smallbun.screw.core.engine.EngineFileType;
import cn.smallbun.screw.core.engine.EngineTemplateType;
import cn.smallbun.screw.core.execute.DocumentationExecute;
import cn.smallbun.screw.core.process.ProcessConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lipor
 * Date 2020/9/10
 */
@RestController
public class generate {

    @Autowired
    private ApplicationContext applicationContext;




    @GetMapping("get")
    public void test(HttpServletRequest httpServletRequest) {
//        testService.add();
//        String forObject = restTemplate.getForObject("http://127.0.0.1:9998/two/2", String.class);
//        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = requestAttributes.getRequest();
        String localAddr = httpServletRequest.getRemoteAddr();
        int serverPort = httpServletRequest.getLocalPort();
         serverPort = httpServletRequest.getServerPort();
        System.out.println("ahahaIp:" + localAddr + "port:" + serverPort);
    }

    public String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    @GetMapping("/generate")
    public String contextLoads() {
        DataSource dataSourceMysql = applicationContext.getBean(DataSource.class);
        // 生成文件配置
        EngineConfig engineConfig = EngineConfig.builder()
            // 生成文件路径，自己mac本地的地址，这里需要自己更换下路径
            .fileOutputDir("/Users/lipor/Desktop")
            // 打开目录
            .openOutputDir(true)
            // 文件类型
            .fileType(EngineFileType.WORD)
            // 生成模板实现
            .produceType(EngineTemplateType.freemarker).build();
        // 生成文档配置（包含以下自定义版本号、描述等配置连接）
        Configuration config = Configuration.builder()
            .version("1.0.4")
            .description("大视频V1.0")
            .dataSource(dataSourceMysql)
            .engineConfig(engineConfig)
            .produceConfig(getProcessConfig())
            .build();
        // 执行生成
        new DocumentationExecute(config).execute();
        return "正在执行====>请稍等";
    }

    /**
     * 配置想要生成的表+ 配置想要忽略的表
     *
     * @return 生成表配置
     */
    public static ProcessConfig getProcessConfig() {
        // 忽略表名
        List<String> ignoreTableName = new ArrayList<>();
        // 忽略表前缀，如忽略a开头的数据库表
        List<String> ignorePrefix = new ArrayList<>();
        // 忽略表后缀
        List<String> ignoreSuffix = new ArrayList<>();
        return ProcessConfig.builder()
            //根据名称指定表生成
            .designatedTableName(new ArrayList<>())
            //根据表前缀生成
            .designatedTablePrefix(new ArrayList<>())
            //根据表后缀生成
            .designatedTableSuffix(new ArrayList<>())
            //忽略表名
            .ignoreTableName(ignoreTableName)
            //忽略表前缀
            .ignoreTablePrefix(ignorePrefix)
            //忽略表后缀
            .ignoreTableSuffix(ignoreSuffix).build();
    }
//
//
//    @GetMapping("{taskId}/counter")
//    public String getData(@PathVariable("taskId") Long taskId){
//        return "你看："+taskId;
//    }
}
